#include "lib.h"

#include "cardmgr.h"

int32
CardMgr::getDeckSize()
{
	return 52;
}

uint8
CardMgr::getSuit(uint8 Card)
{
	return (uint8)(Card & 0xF0);
}

uint8
CardMgr::getRank(uint8 Card)
{
	return (uint8)(Card & 0x0F);
}

uint8
CardMgr::makeCard(int32 Index)
{
	return (uint8)((((Index/13) + 1) << 4) | 
		 ((Index % 13) == 0 ? ACE : ((Index % 13) + 1)));
}

void
CardMgr::encode(uint8 Card, uint8* Buf, int32* Used)
{
	//
	// todo: this implementation is temporary (readable version)
	//

	switch (Card & 0xF0)
	{
		case CLUBS:   Buf[(*Used)++] = 'C'; break;
		case DIAMONDS:Buf[(*Used)++] = 'D'; break;
		case HEARTS:  Buf[(*Used)++] = 'H'; break;
		case SPADES:  Buf[(*Used)++] = 'S'; break;
		default:	  Buf[(*Used)++] = '?'; break;
	}
	switch (Card & 0x0F)
	{
		case ACE:	Buf[(*Used)++] = 'A'; break;
		case 10:	Buf[(*Used)++] = 'T'; break;
		case JACK:	Buf[(*Used)++] = 'J'; break;
		case QUEEN:	Buf[(*Used)++] = 'Q'; break;
		case KING:	Buf[(*Used)++] = 'K'; break;
		default:	Buf[(*Used)++] = (uint8)('0' + ((Card) & 0x0F)); break;
	}
}


// kharmon.  09-20-07.  #16842. 
int32 CardMgr::decode(uint8* pucSource, uint8* pucCard)
{
    int32 ucSuit, ucRank;

    switch (pucSource[0])
    {
        case 'C': ucSuit = CardMgr::CLUBS;        break;
        case 'D': ucSuit = CardMgr::DIAMONDS;     break;
        case 'H': ucSuit = CardMgr::HEARTS;        break;
        case 'S': ucSuit = CardMgr::SPADES;        break;
        default:  ucSuit = 0;                    break;
    }
    switch (pucSource[1])
    {
        case 'A': ucRank = CardMgr::ACE;            break;
        case 'T': ucRank = 10;                  break;
        case 'J': ucRank = CardMgr::JACK;        break;
        case 'Q': ucRank = CardMgr::QUEEN;        break;
        case 'K': ucRank = CardMgr::KING;        break;
        default:  ucRank = pucSource[1] - '0';        break;
    }

    *pucCard = ucSuit | ucRank;

    return 2;
}
